package com.shisfish.library.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.shisfish.library.Impl.BookCategoryImpl;
import com.shisfish.library.Model.BookCategory;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("bookCategory")
public class BookCategoryController extends GlobalVariable {

	BookCategoryImpl bookCategoryImpl = new BookCategoryImpl();
	
	@POST
	@Path("insertOne/{fcode}")
	@Produces("application/json; charset=utf-8")
	public String insertOne(@PathParam("fcode") int fcode,
			@FormParam("name") String name, @FormParam("code") int code, 
			@FormParam("category") String category) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		BookCategory bookCategory = new BookCategory(fcode, code, category, name);
		
		int flag = bookCategoryImpl.insertOne(bookCategory);
		
		if (flag == 1) {
			globalMap.put("message", ADD_SUCCESS);
		}
		else {
			globalMap.put("message", ADD_ERROR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月7日 下午6:52:28
	 * @Descrition : 通过分类查询书本信息（分页）
	 * @param category
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@GET
	@Path("selectByCategory/{category}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByCategory(@PathParam("category") String category, @PathParam("currentPage") int currentPage, 
			@PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		int count = bookCategoryImpl.countByCategory(category);
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			
			List<BookCategory> bookCategorys = bookCategoryImpl.selectByCategory(category, page.getBeginIndex(), pageSize);
			
			if (bookCategorys.size() > 0) {
				globalMap.put("list", bookCategorys);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			} else if (bookCategorys.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
			else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}

		return toJsonUtils.map2Json(globalMap);
		
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月7日 下午6:53:48
	 * @Descrition : 查询全部的分类
	 * @return
	 */
	@GET
	@Path("selectAll")
	@Produces("application/json; charset=utf-8")
	public String selectAll() {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		// 找到父类的信息
		List<BookCategory> categorys = bookCategoryImpl.selectByCode(0);
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for (BookCategory bookCategory : categorys) {
			Map<String, Object> result = new HashMap<String, Object>();
			List<BookCategory> bcates = bookCategoryImpl.selectByCode(bookCategory.getId());
			result.put("one", bookCategory);
			result.put("two", bcates);
			list.add(result);
		}
		globalMap.put("list", list);

		return toJsonUtils.map2Json(globalMap);
	}
	
}
